<?php
class Plugin_Slide_Option extends Ot_Db_Table
{    
    /**
     * Name of the table in the database
     *
     * @var string
     */
    protected $_name = 'tbl_plugin_slideoption';
  
    /**
     * Primary key of the table
     *
     * @var string
     */
    protected $_primary = 'moduleName';
    
    public function getEnabledModules($type)
    {
        $where = $this->getAdapter()->quoteInto('type = ?', $type)
               . ' AND '
               . $this->getAdapter()->quoteInto('status = ?', 'enabled');
        
        $results = $this->fetchAll($where);
        
        if ($results->count() == 0) {
            return array();
        }
        
        $returnData = array();
        foreach ($results as $r) {
            $returnData[] = array(
                'name'      => $r->moduleName,
                'object'    => $this->factory($r->moduleName),
            );
        }
        
        return $returnData;
    }    
        
    public static function factory($plugin)
    {       
        $classname = $plugin . '_Module';
        
        set_include_path(
            get_include_path() . PATH_SEPARATOR . 
            self::getPluginPath()
        );
        
        /*
         * Load the adapter class.  This throws an exception
         * if the specified class cannot be loaded.
         */
        if (!class_exists($classname)) {
            require_once 'Zend/Loader.php';
            Zend_Loader::loadClass($classname);
        }
        
        $adapter = new $classname();
        
        /*
         * Verify that the object created is a descendent of the abstract adapter type.
         */
        if (!$adapter instanceof Plugin_Slide_Option_Interface) {
            throw new Exception("Plugin class '$classname' does not extend Plugin_Slide_Option_Interface");
        }

        return $adapter;
    }
    
    /**
     * gets the basepath to the plugin
     * 
     * @param $plugin
     * @return string
     */
    public static function getPluginPath($plugin = '')
    {
        return realpath(APPLICATION_PATH . '/../plugins/slideoption/' . $plugin);
    }
}